/**
 * 
 */
package fekih.ece.ing4.monte.carlo.singlethread;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

/**
 * @author YacineFekih
 * 
 */
public abstract class AbstractMonteCarlo {
	
	/**
	 * this function reads data from a file 
	 * @return the table params read from the file data.txt 
	 * @throws IOException
	 */
	public static String[] readData() throws IOException {

		final BufferedReader reader = new BufferedReader(new FileReader(
				"data.txt"));

		String[] params = {};
		String data = null;
		while ((data = reader.readLine()) != null) {
			params = data.split(",");
		}

		return params;
	}

	/**
	 * this function execute monte carlo simulation and show time and result
	 * @throws ExecutionException
	 * @throws InterruptedException
	 * @throws IOException
	 */
	public void timeAndComputeValue() throws ExecutionException,
			InterruptedException, IOException {
		final long start = System.nanoTime();

		final String[] paramsMT = readData();
		final double res = computeMonteCarlo(paramsMT);

		final long end = System.nanoTime();

		System.out.println("The result is " + res);
		System.out.println("Time (seconds) taken " + (end - start) / 1.0e9);
	}

	/**
	 * 
	 * @param paramsMT, the monte carlo parameters
	 * @return the result of the monte carlo simulation
	 * @throws ExecutionException
	 * @throws InterruptedException
	 * @throws IOException
	 */
	public abstract double computeMonteCarlo(
			final String[] paramsMT) throws ExecutionException,
			InterruptedException, IOException;

}
